//
//  NetWork.swift
//  Swift_base_grammar
//
//  Created by lantaiyuan on 2017/9/14.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//  02-01网络请求

import Foundation
import UIKit

func datatask() {
    
    //1.创建url对象
    let url = NSURL.init(string: "http://api.3g.ifeng.com/clientShortNews?type=beauty")
    //2.穿件request对象
    let urlRequest:URLRequest = URLRequest.init(url: url! as URL)
    
    //3.创建session会话
    let session = URLSession.shared
    
    // `_` 表示忽略
    // session默认超时时长 60s
    //4.创建任务
   let dataTask = session.dataTask(with: urlRequest) { (data, response, error) in
        if error != nil {
           print(error?.localizedDescription as Any)
        }else{
            do{
               let dict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
               print(dict)
            } catch{
               print(error.localizedDescription.description)
            }
        }
    }
    //5.启动任务
   dataTask.resume()
}
